local sanfa = fk.CreateSkill{
  name = "xiaobai__sanfa",
  dynamic_desc = function (self, player, lang)
    local r = ""
    local previous = #player:getTableMark("xiaobai__sanfa_previous")
    local next = #player:getTableMark("xiaobai__sanfa_next")
    if previous == 1 and next == 1 then 
      if player:usedSkillTimes("xiaobai__dengxian", Player.HistoryGame) == 0 then
        return self.name
      else
        return "xiaobai__sanfa_reverse"
      end
    end
    if previous > 1 and next > 1 then
      r = "xiaobai__sanfa_dynamics:".."xiaobai__sanfa_imediate:".."xiaobai__sanfa_allSlash::"
    elseif previous > 1 then
      r = "xiaobai__sanfa_dynamics:".."xiaobai__sanfa_slash:".."xiaobai__sanfa_allSlash:".."xiaobai__sanfa_if:".."xiaobai__sanfa_noDamage"
    else
      r = "xiaobai__sanfa_dynamics:".."xiaobai__sanfa_imediate:".."xiaobai__sanfa_electSlash:".."xiaobai__sanfa_if:".."xiaobai__sanfa_damage"
    end
    if player:usedSkillTimes("xiaobai__dengxian", Player.HistoryGame) == 0 then
      return r..":"
    else
      return r.."xiaobai__sanfa_reverse"
    end
  end
}

local U = require "packages.utility.utility"

Fk:loadTranslationTable{
  ["xiaobai__sanfa"] = "三法",
  [":xiaobai__sanfa"] = "你可以将一张【杀】当雷【杀】使用，然后若此【杀】：造成伤害，转换牌名添加火【杀】、冰【杀】；"
    .."未造成伤害，转换底牌依次添加基本牌和普通锦囊牌。",
  [":xiaobai__sanfa_reverse"] = "你可以将一张【杀】当雷【杀】使用，然后若此【杀】：造成伤害，转换牌名添加火【杀】、冰【杀】；"
    .."未造成伤害，转换底牌依次添加基本牌和普通锦囊牌。你可以反向转换。",
  [":xiaobai__sanfa_dynamics"] = "你可以将一张{1}当{2}使用{3}{4}{5}。",
  ["xiaobai__sanfa_slash"] = "【杀】",
  ["xiaobai__sanfa_electSlash"] = "雷【杀】",
  ["xiaobai__sanfa_imediate"] = "基本牌或普通锦囊牌",
  ["xiaobai__sanfa_allSlash"] = "雷【杀】、火【杀】或冰【杀】",
  ["xiaobai__sanfa_if"] = "，然后若此【杀】：",
  ["xiaobai__sanfa_damage"] = "造成伤害，转换牌名添加火【杀】、冰【杀】",
  ["xiaobai__sanfa_noDamage"] = "未造成伤害，转换底牌依次添加基本牌和普通锦囊牌。",
  ["xiaobai__sanfa_reverse"] = "你可以反向转换。",
  ["#xiaobai__sanfa_reverse"] = "或反向转换",
  ["#xiaobai__sanfa"] = "三法：你可以将一张%arg当%arg2使用"
}

sanfa:addAcquireEffect(function (self, player, is_start)
  player.room:setPlayerMark(player, "xiaobai__sanfa_previous", {"slash"})
  player.room:setPlayerMark(player, "xiaobai__sanfa_next", {"thunder__slash"})
end)

sanfa:addLoseEffect(function (self, player, is_start)
  player.room:setPlayerMark(player, "xiaobai__sanfa_previous", 0)
  player.room:setPlayerMark(player, "xiaobai__sanfa_next", 0)
end)

sanfa:addEffect("viewas", {
  pattern = ".",
  interaction = function (self, player)
    local previous = player:getTableMark("xiaobai__sanfa_previous")
    local next = player:getTableMark("xiaobai__sanfa_next")
    if player:usedSkillTimes("xiaobai__dengxian", Player.HistoryGame) > 0 then
      table.insertTableIfNeed(next, previous)
    end
    local card_names = player:getViewAsCardNames(sanfa.name, next)
    return UI.CardNameBox{
      choices = card_names,
      all_choices = next
    }
  end,
  prompt = function (self, player, selected_cards, selected)
    local previous = #player:getTableMark("xiaobai__sanfa_previous")
    local next = #player:getTableMark("xiaobai__sanfa_previous")
    if player:usedSkillTimes("xiaobai__dengxian", Player.HistoryGame) == 0 then
      if previous > 1 and next > 1 then
        return "#xiaobai__sanfa:::".."xiaobai__sanfa_imediate:".."xiaobai__sanfa_allSlash"
      elseif previous > 1 then
        return "#xiaobai__sanfa:::".."xiaobai__sanfa_slash:".."xiaobai__sanfa_allSlash"
      elseif next > 1 then
        return "#xiaobai__sanfa:::".."xiaobai__sanfa_imediate:".."xiaobai__sanfa_electSlash"
      else
        return "#xiaobai__sanfa:::".."xiaobai__sanfa_slash:".."xiaobai__sanfa_electSlash"
      end
    else
      if previous > 1 and next > 1 then
        return Fk:translate("#xiaobai__sanfa:::".."xiaobai__sanfa_imediate:"..Fk:translate("xiaobai__sanfa_allSlash"))..Fk:translate("#xiaobai__sanfa_reverse")
      elseif previous > 1 then
        return Fk:translate("#xiaobai__sanfa:::".."xiaobai__sanfa_slash:"..Fk:translate("xiaobai__sanfa_allSlash"))..Fk:translate("#xiaobai__sanfa_reverse")
      elseif next > 1 then
        return Fk:translate("#xiaobai__sanfa:::".."xiaobai__sanfa_imediate:"..Fk:translate("xiaobai__sanfa_electSlash"))..Fk:translate("#xiaobai__sanfa_reverse")
      else
        return Fk:translate("#xiaobai__sanfa:::".."xiaobai__sanfa_slash:"..Fk:translate("xiaobai__sanfa_electSlash"))..Fk:translate("#xiaobai__sanfa_reverse")
      end
    end
  end,
  card_filter = function (self, player, to_select, selected, selected_targets)
    if #selected > 0 then return false end
    local previous = player:getTableMark("xiaobai__sanfa_previous")
    local next = player:getTableMark("xiaobai__sanfa_next")
    if player:usedSkillTimes("xiaobai__dengxian", Player.HistoryGame) == 0 then
      return table.contains(previous, Fk:getCardById(to_select).trueName)
    else
      return (table.contains(previous, Fk:getCardById(to_select).trueName) and table.contains(next, self.interaction.data)) or
        (table.contains(next, Fk:getCardById(to_select).name) and table.contains(previous, self.interaction.data))
    end
  end,
  view_as = function (self, player, cards)
    if #cards == 0 then return end
    local c = Fk:cloneCard(self.interaction.data)
    c:addSubcards(cards)
    c.skillName = sanfa.name
    return c
  end,
  enabled_at_play = function (self, player)
    local names = player:getTableMark("xiaobai__sanfa_next")
    if player:usedSkillTimes("xiaobai__dengxian", Player.HistoryGame) > 0 then
      table.insertTableIfNeed(names ,player:getTableMark("xiaobai__sanfa_previous"))
    end
    for _, cn in ipairs(names) do
      local c = Fk:cloneCard(cn)
      c.skillName = sanfa.name
      if player:canUse(c) then return true end
    end
  end,
  enabled_at_response = function (self, player, response)
    if response then return end
    local names = player:getTableMark("xiaobai__sanfa_next")
    if player:usedSkillTimes("xiaobai__dengxian", Player.HistoryGame) > 0 then
      table.insertTableIfNeed(names ,player:getTableMark("xiaobai__sanfa_previous"))
    end
    for _, cn in ipairs(names) do
      local c = Fk:cloneCard(cn)
      if Exppattern:Parse(Fk.currentResponsePattern):match(c) then return true end
    end
  end
})

sanfa:addEffect(fk.CardUseFinished, {
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(self) and target == player and table.contains(data.card.skillNames, sanfa.name) 
  end,
  on_refresh = function (self, event, target, player, data)
    local e = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard, true)
    if not e then return end
    local use = e.data
    if use.damageDealt and #player:getTableMark("xiaobai__sanfa_next") == 1 then
      player.room:setPlayerMark(player, "xiaobai__sanfa_next", {"thunder__slash", "fire__slash", "ice__slash"})
    elseif not use.damageDealt and #player:getTableMark("xiaobai__sanfa_previous") == 1 then
      player.room:setPlayerMark(player, "xiaobai__sanfa_previous", Fk:getAllCardNames("bt"))
    end
  end
})

return sanfa